<?php
namespace cms\modules\v1\models\user;

use cms\modules\v1\models\Common;
use common\models\Functions;
use common\models\Status;
use yii\base\Exception;
use Yii;
/**
 * Class EmployeeInfo
 * @package cms\modules\v1\models
 */
class UserLoginLog extends Common
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_login_log}}';
    }

    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @param string $search
     * @return array
     */
    public function getListData($page = 1, $size = 50, $search = '') {
        try {

            //搜索条件
            $searchWhere  = $this->getSearch($search);//自定义搜索条件
            $query = new \common\models\user\UserLoginLog();

            $count = 0;
            //得到的总数
            if(isset($searchWhere['andWhere'])){
                $count = $query::getObjectCountByAndWhere($searchWhere['where'], $searchWhere['andWhere'])->count();
            }
            $data = static::setPageinationAPI($page, $size, $count);

            //设置分页
            $offset = ($data['page'] - 1) * $size;

            //排序 order
            $orderSql = 'login_ip DESC, id DESC';

            if(isset($searchWhere['andWhere'])){
                $query = $query::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);
            }

            $data['data'] = $this->handelList($query);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     * @param $search
     * @return string
     */
    public function getSearch ($search = []) {
        $where = [];
        $andWhere = [];
        $search = json_decode($search,true);
        if(!empty($search)){
        }
        return [
            'where' => $where,
            'andWhere' => $andWhere
        ];
    }

    /**
     * 处理初始化数据
     * @param $query
     * @return mixed
     */
    public function handelList($query) {
        if (empty($query)) {
            return null;
        }
//        $userInfo = User::getByWhere([
//            'id' => array_column($list,'user_id'),
//        ]);
//        $userMap = Functions::extractKey($userInfo,'id');
//
        $list = $query->with('user')->asArray()->all();
        foreach ($list as $key => $value) {
            $singleUserInfo = !empty($value['user_id']) && !empty($value['user']) ? $value['user'] : [];
            $list[$key]['login_name'] = '';
            $list[$key]['auth_name'] = '';
            if ($singleUserInfo) {
                $list[$key]['login_name'] = $singleUserInfo['login_name'];
                $list[$key]['nick_name'] = $singleUserInfo['nick_name'] ?: '';
            }
            $list[$key]['time'] = $value['login_time'];
            $list[$key]['position'] = $value['login_position'];
            $list[$key]['ip'] = $value['login_ip'];
        }
        return $list;
    }

}
